#include "gnode.h"
#include "ggraphtable.h"

GNode::GNode(QWidget *parent) :
    QWidget(parent), graphNode(0), velocity(QPointF()), mass(0.1), isSelected(0), isProcessed(0)
{
    QPalette p=palette();
    p.setBrush(QPalette::Background,QBrush(Qt::white));
    setPalette(p);
    setMouseTracking(true);
}

void GNode::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.save();
    QPen pen(QBrush(Qt::black),0.03);
    painter.setPen(pen);
    painter.setBrush(palette().background());
    painter.scale(this->width(),this->height());
    painter.drawEllipse(QPointF(0.5f,0.5f),0.4,0.4);
    painter.restore();
    painter.setFont(QFont("Arial",16));
    if (graphNode)
        painter.drawText(0,0,width(),height(),Qt::AlignCenter | Qt::AlignHCenter, QString::number(graphNode->data));
}

void GNode::mousePressEvent(QMouseEvent *event)
{
    isSelected=!isSelected;
    QPalette p=palette();
    if (isSelected)
    {
        p.setBrush(QPalette::Background,QBrush(Qt::green));
    }
    else
    {
        p.setBrush(QPalette::Background,QBrush(Qt::white));
    }
    setPalette(p);
    update();
}

void GNode::enterEvent(QEvent *)
{
    QPalette p=palette();
    if (!isSelected)
    {
        p.setBrush(QPalette::Background,QBrush(Qt::lightGray));
    }
    else
    {
        p.setBrush(QPalette::Background,QBrush(Qt::darkGreen));
    }
    setPalette(p);
    update();
}

void GNode::leaveEvent(QEvent *)
{
    QPalette p=palette();
    if (!isSelected)
    {
        p.setBrush(QPalette::Background,QBrush(Qt::white));
    }
    else
    {
        p.setBrush(QPalette::Background,QBrush(Qt::green));
    }
    setPalette(p);
    update();
}
